package com.czl.module_storehouse.activity.intostore.project;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_storehouse.databinding.ActivitySelectProjectStoreBinding;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.SelectProjectAdapter;
import com.czl.module_storehouse.bean.StoreProjectBean;
import com.czl.module_storehouse.event.StoreProjectEvent;
import com.czl.module_storehouse.mvp.presenter.AddWareHousePresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class SelectProjectActivity extends BaseActivity<ActivitySelectProjectStoreBinding> {

    private SelectProjectAdapter mAdapter;
    private final List<StoreProjectBean> mList = new ArrayList<>();

    @Override
    protected ActivitySelectProjectStoreBinding getBinding(LayoutInflater layoutInflater) {
        return ActivitySelectProjectStoreBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("选择工程");
        mAdapter = new SelectProjectAdapter(R.layout.item_select_project, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void loadData() {
        super.loadData();
        /*if (mPresenter != null) {
            mPresenter.selectProjectInStoreHouse();
        }*/
    }

    protected void initListener() {
        if (mAdapter != null) {
            mAdapter.setOnItemChildClickListener((adapter, view, position) -> clickItem(view, position));
            mAdapter.setOnItemClickListener((adapter, view, position) -> clickItem(view, position));
        }
        binding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                search(s.toString());
            }
        });
    }

    private void search(String text) {
        if (TextUtils.isEmpty(text)) {
            mAdapter.setList(mList);
            return;
        }
        List<StoreProjectBean> newList = new ArrayList<>();
        for (StoreProjectBean bean : mList) {
            if (bean.getMiniProjectName() != null && bean.getMiniProjectName().contains(text)) {
                newList.add(bean);
            }
        }
        if (newList.isEmpty()) {
            mAdapter.setList(new ArrayList<>());
            View view = View.inflate(getContext(), R.layout.common_empty_layout, null);
            mAdapter.setEmptyView(view);
        } else {
            mAdapter.setList(newList);
        }
    }

    private void clickItem(View view, int position) {
        StoreProjectBean item = mAdapter.getItem(position);
        int id = view.getId();
        if (id == R.id.iv_right || id == R.id.cl_root) {
            nextActivity(item);
        } else if (id == R.id.text_look) {
            nextActivity(item);
        }
    }

    private void nextActivity(StoreProjectBean bean) {
//        EventBus.getDefault().postSticky(new FacilityListEvent(bean.getFacilityList()));
        EventBus.getDefault().postSticky(bean);
        startActivity(new Intent(getContext(), ViewStoreProjectItemActivity.class));
    }

    private void selectItem(StoreProjectBean item) {
        EventBus.getDefault().postSticky(new StoreProjectEvent(item));
        finish();
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        if (t instanceof HttpResponse) {
            String requestTag = ((HttpResponse<?>) t).getRequestTag();
            if (AddWareHousePresenter.TAG_STORE_PROJECT.equals(requestTag)) {
                ListBean<StoreProjectBean> listBean = (ListBean<StoreProjectBean>) ((HttpResponse<?>) t).getData();
                if (listBean != null) {
                    mList.clear();
                    List<StoreProjectBean> list = listBean.getList();
                    if (list != null) {
                        mList.addAll(list);
                        mAdapter.addData(list);
                    }
                }
            }
        }
    }
}
